package com.example.upimageadapter.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.upimageadapter.R;
import com.example.upimageadapter.other.IItemTouchHelperAdapter;
import com.example.upimageadapter.other.MyItemTouchHelperCallback;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;

public class UpImagesListAdapter extends BaseQuickAdapter<String, UpImagesListAdapter.ViewHolder> implements IItemTouchHelperAdapter {

    private AddClickListener addClickListener;
    private Context context;
    private MyItemTouchHelperCallback.OnStartDragListener mDragListener;
    private int maxCount = 0;

    public void setMaxCount(int maxCount) {
        this.maxCount = maxCount;
    }

    public int getMaxCount() {
        return maxCount;
    }

    public void setAddClickListener(AddClickListener addClickListener) {
        this.addClickListener = addClickListener;
    }

    public UpImagesListAdapter(Context context, MyItemTouchHelperCallback.OnStartDragListener mDragListener) {
        super(R.layout.item_up_images_list);
        this.context = context;
        this.mDragListener = mDragListener;
    }

    public UpImagesListAdapter(Context context, MyItemTouchHelperCallback.OnStartDragListener mDragListener, int maxCount) {
        super(R.layout.item_up_images_list);
        this.context = context;
        this.mDragListener = mDragListener;
        this.maxCount = maxCount;
    }

    @Override
    protected void convert(@NonNull ViewHolder helper, String item) {
        if (!TextUtils.isEmpty(item)) {
            Glide.with(context).load(item).into(helper.imageView);
        }

        helper.imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (!TextUtils.isEmpty(item)) {
                    if (mDragListener != null) {
                        mDragListener.onStartDrag(helper);
                    }
                }
                return false;
            }
        });
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_up_images_list, parent, false);
        if (viewType == 1) {
            return new ViewHolder(view);
        } else {
            ImageView ivDelete = view.findViewById(R.id.ivDelete);
            ivDelete.setVisibility(View.INVISIBLE);
            return new ViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position == getData().size() ? 0 : 1;//1.正常图片 0.添加图片
    }

    @Override
    public int getItemCount() {
        if (maxCount != 0 && getData().size() == maxCount) {
            return getData().size();
        } else {
            return getData().size() + 1;
        }
    }

    @Override
    public void addData(@NotNull Collection<? extends String> newData) {
        if (maxCount != 0 && getData().size() + newData.size() == maxCount) {
            getData().addAll(newData);
            notifyItemRangeInserted(maxCount - newData.size() + 1, newData.size());
        } else {
            super.addData(newData);
        }
    }

    @Override
    public String getItem(int position) {
        if (getData().size() == position) {
            return null;
        } else {
            return super.getItem(position);
        }
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        if (toPosition != getItemCount() - 1) {
            Collections.swap(getData(), fromPosition, toPosition);
            notifyItemMoved(fromPosition, toPosition);
        }
    }

    @Override
    public void onItemDismiss(int position) {
        getData().remove(position);
        notifyItemRemoved(position);
    }

    class ViewHolder extends BaseViewHolder {

        ImageView imageView;
        ImageView ivDelete;

        public ViewHolder(View view) {
            super(view);
            imageView = view.findViewById(R.id.imageView);
            ivDelete = view.findViewById(R.id.ivDelete);

            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getLayoutPosition() == getData().size()) {
                        if (null != addClickListener) {
                            addClickListener.click(view);
                        }
                    }
                }
            });

            ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    remove(getAdapterPosition());
                }
            });
        }
    }

    public interface AddClickListener {
        void click(View view);
    }
}
